#include "widget.h"
#include "ui_widget.h"
#include "QDebug"
#include "QRegExpValidator"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //设置push_button无效，通过正则后有效
    ui->pushButton->setEnabled(false);


    // 初始化姓名输入框
    ui->lineEdit_name->setPlaceholderText("请输入你的名字");
    ui->lineEdit_name->setClearButtonEnabled(true);

    // 初始化密码输入框
    ui->lineEdit_password->setPlaceholderText("请输入你的密码");
    ui->lineEdit_password->setEchoMode(QLineEdit::Password);
    ui->lineEdit_password->setClearButtonEnabled(true);

    ui->lineEdit_password_2->setPlaceholderText("请输入你的密码");
    ui->lineEdit_password_2->setEchoMode(QLineEdit::Password);


    // 初始化电话号码输入框
    ui->lineEdit_phone->setPlaceholderText("请输入电话号码");
//    ui->lineEdit_phone->setInputMask("000-000-0000");     // 使用inputmask检测只做简单的格式检测
    ui->lineEdit_phone->setClearButtonEnabled(true);
    ui->lineEdit_phone->setValidator(new QRegExpValidator(QRegExp("^1\\d{10}$")));

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    QString gender = ui->radioButton_male->isChecked() ? "男" : "女";

    qDebug() << "姓名" << ui->lineEdit_name->text()
             << "密码" << ui->lineEdit_password->text()
             << "性别" << gender
             << "电话" << ui->lineEdit_phone->text();
}

void Widget::on_lineEdit_phone_textEdited(const QString &arg1)
{
    qDebug() << arg1;

    QString context = arg1;
    int pos = 0;
    if(ui->lineEdit_phone->validator()->validate(context, pos) == QValidator::Acceptable)
    {
        // 验证通过，设置push_button为启用状态
        ui->pushButton->setEnabled(true);
    }
    else
    {
        ui->pushButton->setEnabled(false);
    }
}

void Widget::on_lineEdit_password_textEdited(const QString &arg1)
{
    const QString& s1 = ui->lineEdit_password->text();
    const QString& s2 = ui->lineEdit_password_2->text();

    if(s1.isEmpty() && s2.isEmpty())
    {
        ui->label_6->setText("密码为空");
    }
    else if(s1 != s2)
    {
        ui->label_6->setText("密码不一致");
    }
    else
    {
        ui->label_6->setText("密码一致");
    }
}

void Widget::on_lineEdit_password_2_textEdited(const QString &arg1)
{
    const QString& s1 = ui->lineEdit_password->text();
    const QString& s2 = ui->lineEdit_password_2->text();

    if(s1.isEmpty() && s2.isEmpty())
    {
        ui->label_6->setText("密码为空");
    }
    else if(s1 != s2)
    {
        ui->label_6->setText("密码不一致");
    }
    else
    {
        ui->label_6->setText("密码一致");
    }
}

void Widget::on_checkBox_toggled(bool checked)
{
    if(checked)
    {
        ui->lineEdit_password->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        ui->lineEdit_password->setEchoMode(QLineEdit::Password);
    }
}
